If exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SaveNotificationDocumentMap]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[SaveNotificationDocumentMap]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/************************************************************************************************
* Name:		SaveNotificationDocumentMap                                                            
* Purpose:	Stored procedure for adding/modifying the notification document mapping

* PARAMETERS  
*	Name			Description
* -------------------------	-----------------------------
* @protocolNumber 		protocolNumber
* @protocolProcessId 		Protocol ProcessId
* @boardId 			boardId
* @boardStatus			boardStatus
* @documentId 			documentId
* @createdByAdmPersonId		createdByAdmPersonId
************************************************************************************************/

CREATE PROCEDURE [dbo].[SaveNotificationDocumentMap]
(
 @protocolNumber varchar(12),
 @protocolProcessId int,
 @boardId int,
 @boardStatus varchar(50),
 @documentId int,
 @createdByAdmPersonId int
)
As BEGIN

		Declare @boardStatusId int 
		Set @boardStatusId =  (Select Id from ProtocolBoardReviewStatusType where Name = @boardStatus)

			IF NOT EXISTS(Select Id from ProtocolDetails_Document_Map 
								where 
									  ProtocolNumber = @protocolNumber
									  and isnull(ProtocolProcessId,0) = @protocolProcessId 
									  and BoardId = @boardId 
									  and BoardStatusId = @boardStatusId
									  and DocumentId = @documentId
						 )
				BEGIN			

					Insert into ProtocolDetails_Document_Map ( ProtocolNumber,
															   ProtocolProcessId, 
															   BoardId, 
															   BoardStatusId,
															   DocumentId,
															   CreatedByAdmPersonId
															   )
														Values	
															   (
																@protocolNumber,
																@protocolProcessId,
																@boardId,
																@boardStatusId,
																@documentId,
																@createdByAdmPersonId
																)
				END	
			Else
				BEGIN
					Update ProtocolDetails_Document_Map Set CreatedByAdmPersonId = @createdByAdmPersonId															
															
				END			
END
